﻿
/**
*@description 添加页面基类
*/
Chenxin.UI.AddPageCommon = Chenxin.extend(null, {
    actionUrl: '',
    /**
    *@description 初始化页面元素事件处理，需重写
    */
    initEvent: Chenxin.emptyFn,
    /**
    *@description 用户自定义页面初始化方法
    */
    customerInit: Chenxin.emptyFn,
    /**
    *@description 创建Form
    */
    createForm: Chenxin.emptyFn,
    createGrid: Chenxin.emptyFn,
    initComponent: function() {

        //创建表格
        if (this.createGrid) {
            this.grid = this.createGrid();
        }
    
        //创建表单
        if (this.createForm) {
            this.form = this.createForm();
        }

        //页面初始化
        if (this.customerInit) {
            this.customerInit();
        }

        //绑定元素页面事件
        if (this.initEvent) {
            this.initEvent();
        }
    }
});

/**
*@description 添加页面窗体基类
*/
Chenxin.UI.AddFormCommon=Chenxin.extend(null,{
    id:'',
	additionalParam:{},
	autoAlertSucc:true,		//保存成功后是否自动弹出保存成功的消息
	autoReset:true,			//保存成功后是否自动重置表单项
	op:'add',
	uploadFile:false,
	fileNames:[],
	saveNames:[],
	comboList:[],
	formValConfig:{},
	initEvent:Chenxin.emptyFn,
	saveSucc:Chenxin.emptyFn,
	saveFail:Chenxin.emptyFn,
	delSucc:Chenxin.emptyFn,
	delFail:Chenxin.emptyFn,
	initFormValidator:Chenxin.emptyFn,
	createComboList:Chenxin.emptyFn,
	manualReset:Chenxin.emptyFn,
	loadDefVal:Chenxin.emptyFn,
	initForm:Chenxin.emptyFn,
	manualPreCheck:Chenxin.emptyFn,
	delFile:function(fileName){
	    for(var idxCnt = 0; idxCnt < this.fileNames.length; idxCnt++ ){
	        if(fileName == this.fileNames[idxCnt]){
	            this.fileNames.splice(idxCnt,1);
	            this.saveNames.splice(idxCnt,1);
	            break;
	        }
	    }
	},
    getParam:function(){
		var json=Chenxin.Fn.getFormJson(this.id,this.comboList);		
		if(this.afterGetFormJson){
			var scope=this.scope
			if(this.scope==window){
				scope=this;
			}
			this.afterGetFormJson.call(scope,json);
		}
		return json;
	},
	checkCombo:function(){
		var count=0;
		for(var i=0;i<this.comboList.length;i++){
			if(!this.comboList[i].validator()){
				count++;
			}
		}
		if(count){
			return false;
		}
		return true;
	},
    formValidator:function(model){
		if(this.initFormValidator){
			if(!jQuery.formValidator.pageIsValid(this.id)||!this.checkCombo()){
				alert('请填写完必填(选)项再进行表单提交！');
				return false;
			}
		}else{
			if(!this.checkCombo()){
				alert('请填写完必填(选)项再进行表单提交！');
				return false;
			}
		}
		if(this.customerFormVal){
			return this.customerFormVal(model);
		}
		return true;
	},
	resetFormValidator:function(){
	    if (this.initFormValidator) {
            jQuery.formValidator.resetTipState(this.id);	
		}
	},
	reset:function(){
		Chenxin.Fn.setFormValue(this.id,this.comboList,{});
		if( this.manualReset!= undefined){
		    this.manualReset();
		}
	},
	save:function(){
		var model=this.getParam();
		
		if(!this.formValidator(model)){	
			Chenxin.Msg.Saving.hide();
			return;
		}
		if( this.manualPreCheck != undefined ){
		    if(this.manualPreCheck() == false){
		        return;
		    }
		}
		var _data={
			op:this.op
		};
		var m=model.toString();
		if(m!='{}'){			
			_data.model=m;
		}
		if(this.uploadFile == true){
		    if(this.fileNames.length > 0){
		        _data.filenames = this.fileNames.toString();
		        _data.savenames = this.saveNames.toString();
		    }
		}
		Chenxin.apply(_data,this.additionalParam);
		var _this=this;
		Chenxin.ajax({
			type: "post",
			dataType: "json",
			url: this.actionUrl,
			data: _data,
			onSuccess: function(data,msg) {
			    Chenxin.Msg.Saving.hide();
			    if(_this.autoAlertSucc){
					alert(msg);
				}
				if(_this.saveSucc){		
					_this.saveSucc.call(_this.scope,data,msg);
				}
				if(_this.autoReset){
					_this.reset.call(_this);
					_this.resetFormValidator.call(_this);		
				}
			},
			onFail:function(data,msg){
				Chenxin.Msg.Saving.hide();	
				if(_this.saveFail){
					_this.saveFail(_this.scope,data,msg);
				}					
			}
		});
    },
    del:function(){
        if( this.currentId == undefined ){
            alert("没有要删除的信息");
            return;
        }
        if( !confirm("确定要删除吗？")){
            return;
        }
        var _data={
			op:this.op,
			RecordId:this.currentId
		}
        var _this=this;
		Chenxin.ajax({
			type: "post",
			dataType: "json",
			url: this.actionUrl,
			data: _data,
			onSuccess: function(data,msg) {
			    Chenxin.Msg.Saving.hide();
			    if(_this.autoAlertSucc){
					alert(msg);
				}
				if(_this.delSucc){		
					_this.delSucc.call(_this.scope,data,msg);
				}
			},
			onFail:function(data,msg){
				Chenxin.Msg.Saving.hide();	
				if(_this.delFail){
					_this.delFail(_this.scope,data,msg);
				}					
			}
		});
    },
    initComponent:function(){
        
        if(!this.id){
			throw new Error('id is need!');
			return; 
		}
		
		//页面初始化
		if(this.initForm){
		    this.initForm();
		}
		
		if(this.createComboList){
			this.comboList=this.createComboList();
		}
		//初始化窗体验证
		if(this.initFormValidator){		
			Chenxin.apply(this.formValConfig,{
			    onsuccess:function(){return true;},
			    validatorgroup:this.id
            });
			jQuery.formValidator.initConfig(this.formValConfig);
			this.initFormValidator();
		}
		//初始化事件
		if(this.initEvent){
			this.initEvent();
		}
		//页面初始化
		if(this.loadDefVal){
		    this.loadDefVal();
		}
    }
});

